/**
 * Created by Lyf on 2020/11/03.
 */
;(function(win, doc) {
    var _global;

    var SelectUser = {
        //layer  layer对象
        //type  查询类型 1.现居住地  2.户籍所在地
        //selectUser  初始化选择用户字符串（字符串，多个用户编号使用逗号隔开）
        "initSelectUser":function(layer,table,type,selectUser,confirmFun,noSelectStr){
            var selectStr = "";
            if(selectUser && selectUser.length > 0){
                SelectUser.selectUserList = selectUser;
                $.each(selectUser,function (i,v) {
                    selectStr += v.user_no + ",";
                })
            }else{
                SelectUser.selectUserList = [];
            }
            if(noSelectStr){
                SelectUser.initNoSelectUserStr = noSelectStr;
            }else{
                SelectUser.initNoSelectUserStr = "";
            }
            SelectUser.type = type;
            SelectUser.initSelectUserStr = selectStr;
            SelectUser.openLayer(layer,table,confirmFun,selectUser);
        },

        "orgList":null, //组织架构
        "basePath":$("input#basePathInput",parent.document).val(), //组织架构
        "orgNo":$("input#orgIdInput").val(), //组织架构
        //"basePath":"http://localhost:8088/ATPension", //组织架构
        "type":"1",  //类型 1.现居住地  2.户籍所在地
        "initSelectUserStr":"",  //初始化选择用户
        "initNoSelectUserStr":"",  //初始化不能选择的用户
        "selectUserList":[],  //选择的用户

        /**
         * 初始化弹框
         */
        "openLayer":function(layer,table,confirmFun,selectUser){
            var html = SelectUser.getLayerOpenHtml(layer);
            if(html != "error") {
                var selectUserIndex = layer.open({
                    type: 1,
                    title: false, //不显示标题栏
                    closeBtn: false,
                    area: ['1200px', '800px'],
                    shade: 0.6,
                    id: 'payCons', //设定一个id，防止重复弹出
                    btnAlign: 'c',
                    moveType: 1, //拖拽模式，0或者1
                    shadeClose: false, //设置允许点遮罩关闭
                    content: html,
                    success: function () {
                        //关闭弹层
                        $(".selectUser_cancel").on("click",function () {
                            SelectUser.initSelectUserStr = "";
                            SelectUser.selectUserList = [];
                            layer.close(selectUserIndex);
                        });

                        $("span#selectUser_submit").on("click",function (e) {
                            if(typeof confirmFun == "function"){
                                confirmFun();
                            }
                            SelectUser.initSelectUserStr = "";
                            SelectUser.selectUserList = [];
                            layer.close(selectUserIndex);
                        })

                        //第一个实例
                        table.render({
                            elem: '#selectUser_table',
                            url: '../common/getUserList',
                            method:"post",
                            request: {
                            pageName: 'page', //页码的参数名称，默认：page
                            limitName: 'pageSize' //每页数据量的参数名，默认：limit
                            },
                            where:{ orgNo: SelectUser.orgNo},
                            id:"select_user_table",
                            done:function(res, curr, count){
                                if(selectUser && selectUser.length > 0) {
                                    $("td[data-field='user_no'] div.layui-table-cell",$("div[lay-id='select_user_table'] > div > div.layui-table-fixed > div.layui-table-body > table")).each(function (i, v) {
                                        var that = this;
                                        var val = $(this).text();
                                        if(SelectUser.initNoSelectUserStr.indexOf(val) != -1){
                                            $(that).parent("td").prev("td").find("div.layui-form-checkbox").addClass("layui-radio-disbaled layui-disabled");
                                        }else if(SelectUser.initSelectUserStr.indexOf(val) != -1){
                                            $(that).parent("td").prev("td").find("div.layui-form-checkbox").addClass("layui-form-checked");
                                        }
                                    })
                                }
                            },
                            page: true,
                            cols: [
                                [ //表头
                                    {
                                        type: 'checkbox',
                                        fixed: 'left',
                                        width:"5%"
                                    }, {
                                    field: 'user_no',
                                    title: '编号',
                                    fixed: 'left',
                                    hide: true
                                }, {
                                    field: 'user_type',
                                    title: '人员类别',
                                    fixed: 'left',
                                    hide: true
                                }, {
                                    field: 'user_type_code',
                                    title: '人员类别代码',
                                    fixed: 'left',
                                    hide: true
                                }, {
                                    field: 'user_name',
                                    title: '姓名',
                                    width:"10%"
                                }, {
                                    field: 'user_sex',
                                    title: '性别',
                                    width:"8%"
                                }, {
                                    field: 'age',
                                    title: '年龄',
                                    width:"8%"
                                }, {
                                    field: 'household_register',
                                    title: '户籍类型',
                                    width:"10%"
                                }, {
                                    field: 'register_area_of_residence',
                                    title: '户籍所在地',
                                    templet: function(d){
                                        var val = d.register_area_of_residence;
                                        if(val){
                                            return val.replace("/","").replace(/,/g,"");
                                        }else{
                                            return "";
                                        }

                                    },
                                    width:"20%"
                                }, {
                                    field: 'live_address',
                                    title: '现居住地址',
                                    width:"19%"
                                }, {
                                    field: 'live_area_of_residence',
                                    title: '居住地区',
                                    templet: function(d){
                                        var val = d.live_area_of_residence;
                                        if(val){
                                            return val.replace("/","").replace(/,/g,"");
                                        }else{
                                            return "";
                                        }

                                    },
                                    width:"20%"
                                }
                                ]
                            ]
                        });

                        table.on('checkbox(select_user_table)', function(obj){
                            if(obj.type == "all"){
                                var tableAll = layui.table.cache.select_user_table;
                                if(obj.checked){
                                    if(tableAll && tableAll.length > 0){
                                        $.each(tableAll,function (i,v) {
                                            if(! (SelectUser.initSelectUserStr.indexOf(v.user_no) != -1)){
                                                SelectUser.initSelectUserStr += v.user_no + ",";
                                                SelectUser.selectUserList.push(v);
                                            }
                                        })
                                    }
                                }else{
                                    if(tableAll && tableAll.length > 0){
                                        $.each(tableAll,function (i,v) {
                                            $.each(SelectUser.selectUserList,function (j,w) {
                                                if(v.user_no == w.user_no){
                                                    SelectUser.initSelectUserStr = SelectUser.initSelectUserStr.replace(v.user_no + ",","");
                                                    SelectUser.selectUserList.splice(j,1);
                                                    return false;
                                                }
                                            })
                                        })
                                    }
                                }
                            }else if(obj.type == "one"){
                                if(obj.checked){
                                    if(! (SelectUser.initSelectUserStr.indexOf(obj.data.user_no) != -1)){
                                        SelectUser.initSelectUserStr += obj.data.user_no + ",";
                                        SelectUser.selectUserList.push(obj.data);
                                    }
                                }else{
                                    $.each(SelectUser.selectUserList,function (j,w) {
                                        if(w.user_no == obj.data.user_no){
                                            SelectUser.initSelectUserStr = SelectUser.initSelectUserStr.replace(w.user_no + ",","");
                                            SelectUser.selectUserList.splice(j,1);
                                            return false;
                                        }
                                    })
                                }
                            }
                        });


                        $("span#select_user_search_button").on("click",function (e) {
                            table.reload('select_user_table', {
                                where: { //设定异步数据接口的额外参数，任意设
                                    orgNo: SelectUser.orgNo
                                    ,userName: $("input#selectUser_name_search").val()
                                }
                                ,page: {
                                    curr: 1
                                }
                            });
                        })
                    }
                });
            }
        },

        "getLayerOpenHtml":function (layer) {
            var html = '<div id="selectUserLayer"><div class="pop-up"><div><div class="border-bot1px pb20px mt10px relative"><div class="add-mess"><div class="add-mess-child left"><div><label class="f14px">关键字：</label></div>';
            html += '</div><div class="add-mess-form-border left lh32px relative ml30px"><p><input type="text" id="selectUser_name_search" placeholder="请输入姓名关键字"></p><span id="select_user_search_button"><img src="'+SelectUser.basePath+'/static/img/icon_search.png"></span></div></div><p class="add-mess-pos selectUser_cancel"><img src="'+SelectUser.basePath+'/static/img/close_icon.png" class="block"><p></div><div><div class="overflow mt20px mb20px"><p class="left f16px">添加老人</p><p class="right">共有<span class="red" id="selectUser_count"></span>条数据</p></div><div class="border1px border-radius4"><table class="layui-hide" id="selectUser_table" lay-filter="select_user_table"></table></div></div><div class="btn-con textCenter mt20px"><span style="cursor: pointer" class="btn-one selectUser_cancel">取&nbsp;消</span><span class="btn-two" style="cursor: pointer" id="selectUser_submit">提&nbsp;交</span></div></div></div>';
            return html;
        },
        "getOrgList":function (layer) {
            var data = null;
            $.ajax({
                url:"../org/getAllAuthTypeOrg",
                type:"post",
                dataType:"json",
                async:false,
                data:{"type":"3","userFlag":"yes"},
                success:function (result) {
                    if(result.children && result.children.length > 0){
                        data = result.children;
                    }else{
                        data = "error";
                        layer.msg("未获取到组织机构数据！");
                    }
                }
            })
            return data;
        }
    }

    _global = (function(){ return this || (0, eval)('this'); }());

    if (typeof module !== "undefined" && module.exports) {

        module.exports = SelectUser;

    } else if (typeof define === "function" && define.amd) {

        define(function(){return SelectUser;});

    } else {

        !('SelectUser' in _global) && (_global.SelectUser = SelectUser);

    }

})(window, document);